#include "CSVparser.h"

std::string const& CSVRow::operator[](std::size_t index) const 
{
	return m_data[index];
}

std::size_t CSVRow::size() const
{
	return m_data.size();
}

void CSVRow::readNextRow(std::istream& str)
{
	std::string			line;
	std::getline(str,line);

	std::stringstream	lineStream(line);
	std::string			cell;

	m_data.clear();
	while(std::getline(lineStream,cell,'\t'))
	{
		m_data.push_back(cell);
	}
}

CSVIterator::CSVIterator(std::istream& str) : m_str(str.good()?&str:NULL) {
	++(*this);
}

CSVIterator::CSVIterator() :m_str(NULL) { }

CSVIterator& CSVIterator::operator++() {
	if (m_str) {
		(*m_str) >> m_row;m_str = m_str->good()?m_str:NULL;
	}
	return *this;
}

CSVIterator CSVIterator::operator++(int) {
	CSVIterator tmp(*this);
	++(*this);
	return tmp;
}

CSVRow const& CSVIterator::operator*() const {
	return m_row;
}

CSVRow const* CSVIterator::operator->() const {
	return &m_row;
}

bool CSVIterator::operator==(CSVIterator const& rhs) {
	return ((this == &rhs) || ((this->m_str == NULL) && (rhs.m_str == NULL)));
}

bool CSVIterator::operator!=(CSVIterator const& rhs) {
	return !((*this) == rhs);
}

std::istream& operator>>(std::istream& str,CSVRow& data) {
	data.readNextRow(str);
	return str;
}
